﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
using System.Transactions;

namespace My
{
    /// <summary>
    /// MyFormView 的摘要描述
    /// </summary>
    public class MyFormView : FormView
    {
        public MyFormView()
        {
            //
            // TODO: 在此加入建構函式的程式碼
            //            
        }

        protected override void OnItemCommand(FormViewCommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case "MySave":
                    OnSaving(new MyFormViewSavingEventArgs());
                    break;
                default:
                    base.OnItemCommand(e);
                    break;
            }
        }

        protected void OnSaving(MyFormViewSavingEventArgs e)
        {
            using (TransactionScope scope = new TransactionScope())
            {
                Saving(this, e);

                if (e.IsValid == false)
                {
                    return;
                }

                scope.Complete();
            }

            Saved(this, EventArgs.Empty);
        }

        public event EventHandler<MyFormViewSavingEventArgs> Saving;
        public event EventHandler Saved;
    }

    public class MyFormViewSavingEventArgs : EventArgs
    {
        public bool IsValid { get; set; }

        public MyFormViewSavingEventArgs() : this(true)
        {
        }

        public MyFormViewSavingEventArgs(bool isValid)
        {
            this.IsValid = isValid;
        }
    }
}